package com.plian.system.service.zzd.bank.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.plian.Tools.CollectionUtil;
import com.plian.system.entity.zzd.ReportBalance;
import com.plian.system.mapper.zzd.ReportBalanceMapper;
import com.plian.system.service.zzd.bank.IReportBalanceService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @Description: TODO
 * @Author: Take-off
 * @Date: 2023/6/2 2:30 PM
 * @Param
 * @return
 * @Version: 1.0
 **/
@Service
@AllArgsConstructor
public class ReportBalanceServiceImpl extends ServiceImpl<ReportBalanceMapper, ReportBalance> implements IReportBalanceService {


    @Override
    public List<ReportBalance> getBalances(Date date){
        List<ReportBalance> result = new ArrayList<>();

        List<ReportBalance> reportBalances = list(Wrappers.<ReportBalance>lambdaQuery().le(ReportBalance::getBalDate, date));
        if (CollectionUtil.isNotEmpty(reportBalances)){
            Map<String, List<ReportBalance>> map = reportBalances.stream().filter(m -> Optional.ofNullable(m.getOurCorpCode()).isPresent()).collect(Collectors.groupingBy(ReportBalance::getOurCorpCode));
            for (String key :map.keySet()){
                ReportBalance reportBalance = map.get(key).stream().max(Comparator.comparing(ReportBalance::getBalDate)).get();
                result.add(reportBalance);
            }
        }

        return result;
    }

}
